package com.fanfan.SmartPensionSystembackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fanfan.SmartPensionSystembackend.entity.BaseInfo;
import com.fanfan.SmartPensionSystembackend.entity.Elder;
import org.apache.ibatis.annotations.*;

@Mapper
public interface ElderMapper extends BaseMapper<Elder> {
    @Select("SELECT MAX(elderId) FROM elder")
    String getMaxElderId();

    @Insert({
            "INSERT INTO elder (elderId, name, gender, birthDate, domicile, idCard, height, weight, nation, maritalStatus, diseaseHistory, healthStatus, admissionTime, bedId)",
            "VALUES (#{elderId}, #{baseInfo.name}, #{baseInfo.gender}, #{baseInfo.birthDate}, #{baseInfo.domicile}, #{baseInfo.idCard}, #{baseInfo.height}, #{baseInfo.weight}, #{baseInfo.nation}, #{baseInfo.maritalStatus}, #{baseInfo.diseaseHistory}, #{baseInfo.healthStatus}, #{baseInfo.admissionTime}, #{baseInfo.bedId})"
    })
    void insertElder(Elder elder);

    //根据用户id查询所有
    @Select("SELECT * FROM elder WHERE elderId = #{elderId}")
    BaseInfo selectById(String elderId);

    // 更新老人的信息
    @Update({
        "UPDATE elder SET name = #{name}, gender = #{gender}, birthDate = #{birthDate}, domicile = #{domicile}, ",
        "idCard = #{idCard}, height = #{height}, weight = #{weight}, nation = #{nation}, maritalStatus = #{maritalStatus}, ",
        "diseaseHistory = #{diseaseHistory}, healthStatus = #{healthStatus}, admissionTime = #{admissionTime} ",
        "WHERE elderId = #{elderId}"
    })
    void updateElderInfo(BaseInfo baseInfo);

    // 更新老人的信息（bedId版）
    @Update({
            "UPDATE elder SET name = #{name}, gender = #{gender}, birthDate = #{birthDate}, domicile = #{domicile}, ",
            "idCard = #{idCard}, height = #{height}, weight = #{weight}, nation = #{nation}, maritalStatus = #{maritalStatus}, ",
            "diseaseHistory = #{diseaseHistory}, healthStatus = #{healthStatus}, admissionTime = #{admissionTime},bedId=#{bedId}",
            "WHERE elderId = #{elderId}"
    })
    void updateElderInfoWidthBedId(BaseInfo baseInfo);

    // 新增：根据 elderId 删除老人信息
    @Delete("DELETE FROM elder WHERE elderId = #{elderId}")
    void deleteById(String elderId);
}